package com.sdhs.paas.codegenerate.utils;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.net.URL;
import java.util.HashMap;

import org.yaml.snakeyaml.TypeDescription;
import org.yaml.snakeyaml.Yaml;
import org.yaml.snakeyaml.constructor.Constructor;

import com.sdhs.paas.codegenerate.bean.CodeGenerateBean;
import com.sdhs.paas.codegenerate.bean.Column;
import com.sdhs.paas.codegenerate.bean.VueGenerateBean;

import freemarker.template.Configuration;

/**
 * @author 官江涛
 * @version 1.0
 * @Title: 自定义yaml文件解析器
 * @date 2018/7/24/9:48
 */
public class FreeMarkerConfigUtils {

    /**
     *  自定义yaml文件解析器
     * @param configFilePath  文件名称
     * @return yaml文件中的数据，为一个对象。
     */
    public static HashMap getConfig(String configFilePath){
        Yaml yaml = new Yaml();
        HashMap<String,String> config = new HashMap<>();
        final String fileName = configFilePath;
        System.out.println(fileName);
        URL url = Configuration.class.getClassLoader().getResource(fileName);
        File conf = new File(url.getFile());
        try {
            config =(HashMap)yaml.load(new FileInputStream(conf));
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        return config;
    }
    
    @SuppressWarnings("deprecation")
	public CodeGenerateBean getYamlBean(String configFilePath){
    	Constructor cons = new Constructor(CodeGenerateBean.class);
        TypeDescription td = new TypeDescription(CodeGenerateBean.class);
        td.putListPropertyType("columns", Column.class);
        cons.addTypeDescription(td);
        Yaml yaml = new Yaml(cons);
        CodeGenerateBean ret = (CodeGenerateBean) yaml.load(this.getClass().getClassLoader()
                .getResourceAsStream(configFilePath));
        return ret;

    }
    
    @SuppressWarnings("deprecation")
   	public VueGenerateBean getVueYamlBean(String configFilePath){
           Constructor cons = new Constructor(VueGenerateBean.class);
           TypeDescription td = new TypeDescription(VueGenerateBean.class);
//           td.putListPropertyType("columns", Column.class);
           td.addPropertyParameters("columns", Column.class);
           cons.addTypeDescription(td);
           Yaml yaml = new Yaml(cons);
           VueGenerateBean ret = (VueGenerateBean) yaml.load(this.getClass().getClassLoader()
                   .getResourceAsStream(configFilePath));
           return ret;

       }

}
